package org.weasel.views;

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.part.ViewPart;
import org.weasel.business.UserStory;
import org.weasel.controllers.UserStoryViewController;

public class UserStoryDetail extends ViewPart {
	public static final String ID = "org.weasel.views.UserStoryDetail";
	private UserStoryViewController controller;  //  @jve:decl-index=0:
	private Composite top = null;
	private Label label = null;
	private Text txtName = null;
	private Label label1 = null;
	private Text textAreaDescription = null;
	private Label label2 = null;
	private Combo comboBusinessValue = null;
	private Label label3 = null;
	private Label label4 = null;
	private Label label5 = null;
	private Combo comboRisk = null;
	private Text txtUsedTime = null;
	private Text txtEstimatedTime = null;
	private Label lblEstimatedTimeInfo = null;
	private Label lblUsedTimeInfo = null;
	private TaskListComposite taskListUI = null;
	private DeveloperComposite developerComposite = null;
	@Override
	public void createPartControl(Composite parent) {
		// TODO Auto-generated method stub
		top = new Composite(parent, SWT.NONE);
		top.setLayout(null);
		//set the controller for this view
		label = new Label(top, SWT.NONE);
		label.setBounds(new Rectangle(14, 15, 95, 22));
		label.setText("Name:");
		txtName = new Text(top, SWT.BORDER);
		txtName.setBounds(new Rectangle(113, 15, 309, 22));
		txtName.addFocusListener(new org.eclipse.swt.events.FocusAdapter() {
			public void focusLost(org.eclipse.swt.events.FocusEvent e) {
				controller.updateStoryName(txtName.getText());
			}
		});
		label1 = new Label(top, SWT.NONE);
		label1.setBounds(new Rectangle(14, 44, 94, 17));
		label1.setText("Description:");
		textAreaDescription = new Text(top, SWT.MULTI | SWT.WRAP | SWT.V_SCROLL | SWT.BORDER);
		textAreaDescription.setBounds(new Rectangle(13, 64, 409, 159));
		textAreaDescription.addFocusListener(new org.eclipse.swt.events.FocusAdapter() {
			public void focusLost(org.eclipse.swt.events.FocusEvent e) {
				controller.updateStoryDescription(textAreaDescription.getText());
			}
		});
		label2 = new Label(top, SWT.NONE);
		label2.setBounds(new Rectangle(449, 18, 116, 18));
		label2.setText("Business value:");
		createCombo();
		label3 = new Label(top, SWT.NONE);
		label3.setBounds(new Rectangle(448, 130, 118, 23));
		label3.setText("Estimated Time:");
		label4 = new Label(top, SWT.NONE);
		label4.setBounds(new Rectangle(449, 197, 120, 26));
		label4.setText("Used Time:");
		label5 = new Label(top, SWT.NONE);
		label5.setBounds(new Rectangle(449, 70, 114, 24));
		label5.setText("Risk:");
		createCombo2();
		top.addDisposeListener(new org.eclipse.swt.events.DisposeListener() {
			public void widgetDisposed(org.eclipse.swt.events.DisposeEvent e) {
				controller.doViewClosedActions();
			}
		});
		txtUsedTime = new Text(top, SWT.BORDER);
		txtUsedTime.setBounds(new Rectangle(573, 196, 96, 27));
		txtEstimatedTime = new Text(top, SWT.BORDER);
		txtEstimatedTime.setBounds(new Rectangle(573, 130, 94, 27));
		lblEstimatedTimeInfo = new Label(top, SWT.NONE);
		lblEstimatedTimeInfo.setBounds(new Rectangle(667, 133, 25, 19));
		lblEstimatedTimeInfo.setText("");
		lblEstimatedTimeInfo.setVisible(false);
		lblEstimatedTimeInfo.setImage(new Image(Display.getCurrent(), getClass().getResourceAsStream("/icons/error.gif")));
		lblUsedTimeInfo = new Label(top, SWT.NONE);
		lblUsedTimeInfo.setBounds(new Rectangle(670, 198, 21, 22));
		lblUsedTimeInfo.setImage(new Image(Display.getCurrent(), getClass().getResourceAsStream("/icons/error.gif")));
		lblUsedTimeInfo.setVisible(false);
		lblUsedTimeInfo.setText("");
		txtEstimatedTime.addFocusListener(new org.eclipse.swt.events.FocusAdapter() {
			public void focusLost(org.eclipse.swt.events.FocusEvent e) {
				controller.updateStoryEstimatedTime(txtEstimatedTime.getText());
			}
		});
		txtUsedTime.addFocusListener(new org.eclipse.swt.events.FocusAdapter() {
			public void focusLost(org.eclipse.swt.events.FocusEvent e) {
				controller.updateStoryUsedTime(txtUsedTime.getText());
			}
		});
		controller = UserStoryViewController.instance();
		taskListUI = controller.createTaskListComposite(top, SWT.NONE);
		taskListUI.setBounds(new Rectangle(13, 238, 679, 163));
		developerComposite = controller.createDeveloperComposite(top, SWT.NONE);
		developerComposite.setBounds(new Rectangle(20, 420, 658, 155));
	}
	

	@Override
	public void setFocus() {
		// TODO Auto-generated method stub

	}
	
	/**
	 * This method shows all the details of a user story passed as parameter on
	 * the user story detail view.
	 * @param userStory
	 */
	public void showUserStoryInformation(UserStory userStory) {
		//setting the name of the userstory
		this.setPartName(userStory.getName());
		
		//displaying user-story information
		txtName.setText(userStory.getName());
		textAreaDescription.setText(userStory.getDescription());
		comboBusinessValue.setText(String.valueOf(userStory.getBusinessValue()));
		comboRisk.setText(String.valueOf(userStory.getRisk()));
		txtEstimatedTime.setText(String.valueOf(userStory.getEstimatedTime()));
		txtUsedTime.setText(userStory.getUsedTime().toString());
		
	}

	/**
	 * This method initializes combo	
	 *
	 */
	private void createCombo() {
		comboBusinessValue = new Combo(top, SWT.NONE);
		comboBusinessValue.setBounds(new Rectangle(570, 15, 92, 25));
		comboBusinessValue.addFocusListener(new org.eclipse.swt.events.FocusAdapter() {
			public void focusLost(org.eclipse.swt.events.FocusEvent e) {
				controller.updateStoryBusinessValue(comboBusinessValue.getText());
			}
		});
		comboBusinessValue.add("1");
		comboBusinessValue.add("2");
		comboBusinessValue.add("3");
	}

	/**
	 * This method initializes combo2	
	 *
	 */
	private void createCombo2() {
		comboRisk = new Combo(top, SWT.NONE);
		comboRisk.setBounds(new Rectangle(572, 71, 94, 31));
		comboRisk.addFocusListener(new org.eclipse.swt.events.FocusAdapter() {
			public void focusLost(org.eclipse.swt.events.FocusEvent e) {
				controller.updateStoryRisk(comboRisk.getText());
			}
		});
		comboRisk.add("1");
		comboRisk.add("2");
		comboRisk.add("3");
	}

	public Label getLblEstimatedTimeInfo() {
		return lblEstimatedTimeInfo;
	}
	
	public void changeEstimatedTimeVisibility(boolean visibility){
		lblEstimatedTimeInfo.setVisible(visibility);
	}

	public Label getLblUsedTimeInfo() {
		return lblUsedTimeInfo;
	}
	

}  //  @jve:decl-index=0:visual-constraint="10,10,730,650"
